home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / DEMOS / MULTITEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-01  |  6.4 KB  |  299 lines

  1. /* $Id: multitex.c,v 3.2 1998/04/01 02:57:49 brianp Exp $ */
  2.  
  3. /*
  4.  * Multitexture demo
  5.  * Brian Paul  February 1998  This program is in the public domain.
  6.  */
  7.  
  8. /*
  9.  * $Log: multitex.c,v $
  10.  * Revision 3.2  1998/04/01 02:57:49  brianp
  11.  * added extension test in Init()
  12.  *
  13.  * Revision 3.1  1998/02/21 00:59:17  brianp
  14.  * changed background color
  15.  *
  16.  * Revision 3.0  1998/02/20 04:42:06  brianp
  17.  * initial rev
  18.  *
  19.  */
  20.  
  21.  
  22. #include <math.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <GL/glut.h>
  27.  
  28. #include "../util/readtex.c"   /* I know, this is a hack. */
  29.  
  30.  
  31. #define TEX0 1
  32. #define TEX1 2
  33. #define TEXBOTH 3
  34. #define ANIMATE 10
  35. #define QUIT 100
  36.  
  37. static GLboolean Animate = GL_TRUE;
  38.  
  39. static GLfloat Drift = 0.0;
  40. static GLfloat Xrot = 20.0, Yrot = 30.0, Zrot = 0.0;
  41. static GLfloat DXrot = 1.0, DYrot = 2.5;
  42.  
  43.  
  44. static void Idle( void )
  45. {
  46.    if (Animate) {
  47.       Drift += 0.05;
  48.  
  49. #ifdef GL_SGIS_multitexture
  50.       glSelectTextureSGIS(GL_TEXTURE0_SGIS);
  51. #endif
  52.       glMatrixMode(GL_TEXTURE);
  53.       glLoadIdentity();
  54.       glTranslatef(Drift, 0.0, 0.0);
  55.       glMatrixMode(GL_MODELVIEW);
  56.  
  57. #ifdef GL_SGIS_multitexture
  58.       glSelectTextureSGIS(GL_TEXTURE1_SGIS);
  59. #endif
  60.       glMatrixMode(GL_TEXTURE);
  61.       glLoadIdentity();
  62.       glTranslatef(0.0, Drift, 0.0);
  63.       glMatrixMode(GL_MODELVIEW);
  64.  
  65.       glutPostRedisplay();
  66.    }
  67. }
  68.  
  69.  
  70. static void DrawObject(void)
  71. {
  72.    glBegin(GL_QUADS);
  73.  
  74. #ifdef GL_SGIS_multitexture
  75.    glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, 0.0, 0.0);
  76.    glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, 0.0, 0.0);
  77.    glVertex2f(-1.0, -1.0);
  78.  
  79.    glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, 2.0, 0.0);
  80.    glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, 1.0, 0.0);
  81.    glVertex2f(1.0, -1.0);
  82.  
  83.    glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, 2.0, 2.0);
  84.    glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, 1.0, 1.0);
  85.    glVertex2f(1.0, 1.0);
  86.  
  87.    glMultiTexCoord2fSGIS(GL_TEXTURE0_SGIS, 0.0, 2.0);
  88.    glMultiTexCoord2fSGIS(GL_TEXTURE1_SGIS, 0.0, 1.0);
  89.    glVertex2f(-1.0, 1.0);
  90. #else
  91.    glTexCoord2f(0.0, 0.0);
  92.    glVertex2f(-1.0, -1.0);
  93.  
  94.    glTexCoord2f(1.0, 0.0);
  95.    glVertex2f(1.0, -1.0);
  96.  
  97.    glTexCoord2f(1.0, 1.0);
  98.    glVertex2f(1.0, 1.0);
  99.  
  100.    glTexCoord2f(0.0, 1.0);
  101.    glVertex2f(-1.0, 1.0);
  102. #endif
  103.  
  104.    glEnd();
  105. }
  106.  
  107.  
  108.  
  109. static void Display( void )
  110. {
  111.    glClear( GL_COLOR_BUFFER_BIT );
  112.  
  113.    glPushMatrix();
  114.       glRotatef(Xrot, 1.0, 0.0, 0.0);
  115.       glRotatef(Yrot, 0.0, 1.0, 0.0);
  116.       glRotatef(Zrot, 0.0, 0.0, 1.0);
  117.       glScalef(5.0, 5.0, 5.0);
  118.       DrawObject();
  119.    glPopMatrix();
  120.  
  121.    glutSwapBuffers();
  122. }
  123.  
  124.  
  125. static void Reshape( int width, int height )
  126. {
  127.    glViewport( 0, 0, width, height );
  128.    glMatrixMode( GL_PROJECTION );
  129.    glLoadIdentity();
  130.    glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 );
  131.    /*glOrtho( -6.0, 6.0, -6.0, 6.0, 10.0, 100.0 );*/
  132.    glMatrixMode( GL_MODELVIEW );
  133.    glLoadIdentity();
  134.    glTranslatef( 0.0, 0.0, -70.0 );
  135. }
  136.  
  137.  
  138. static void ModeMenu(int entry)
  139. {
  140.    GLboolean enable0 = GL_FALSE, enable1 = GL_FALSE;
  141.    if (entry==TEX0) {
  142.       enable0 = GL_TRUE;
  143.    }
  144.    else if (entry==TEX1) {
  145.       enable1 = GL_TRUE;
  146.    }
  147.    else if (entry==TEXBOTH) {
  148.       enable0 = GL_TRUE;
  149.       enable1 = GL_TRUE;
  150.    }
  151.    else if (entry==ANIMATE) {
  152.       Animate = !Animate;
  153.    }
  154.    else if (entry==QUIT) {
  155.       exit(0);
  156.    }
  157.  
  158.    if (entry != ANIMATE) {
  159. #ifdef GL_SGIS_multitexture
  160.       glSelectTextureSGIS(GL_TEXTURE0_SGIS);
  161. #endif
  162.       if (enable0) {
  163.          glEnable(GL_TEXTURE_2D);
  164.       }
  165.       else
  166.          glDisable(GL_TEXTURE_2D);
  167.  
  168. #ifdef GL_SGIS_multitexture
  169.       glSelectTextureSGIS(GL_TEXTURE1_SGIS);
  170. #endif
  171.       if (enable1) {
  172.          glEnable(GL_TEXTURE_2D);
  173.       }
  174.       else
  175.          glDisable(GL_TEXTURE_2D);
  176.    }
  177.  
  178.    glutPostRedisplay();
  179. }
  180.  
  181.  
  182. static void Key( unsigned char key, int x, int y )
  183. {
  184.    switch (key) {
  185.       case 27:
  186.          exit(0);
  187.          break;
  188.    }
  189.    glutPostRedisplay();
  190. }
  191.  
  192.  
  193. static void SpecialKey( int key, int x, int y )
  194. {
  195.    float step = 3.0;
  196.  
  197.    switch (key) {
  198.       case GLUT_KEY_UP:
  199.          Xrot += step;
  200.          break;
  201.       case GLUT_KEY_DOWN:
  202.          Xrot -= step;
  203.          break;
  204.       case GLUT_KEY_LEFT:
  205.          Yrot += step;
  206.          break;
  207.       case GLUT_KEY_RIGHT:
  208.          Yrot -= step;
  209.          break;
  210.    }
  211.    glutPostRedisplay();
  212. }
  213.  
  214.  
  215. static void Init( void )
  216. {
  217.    const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  218.    if (!strstr(exten, "GL_SGIS_multitexture")) {
  219.       printf("Sorry, GL_SGIS_multitexture not supported by this renderer.\n");
  220.       exit(1);
  221.    }
  222.  
  223.    /* setup textur env 0 */
  224. #ifdef GL_SGIS_multitexture
  225.    glSelectTextureSGIS(GL_TEXTURE0_SGIS);
  226. #endif
  227. #ifdef LINEAR_FILTER
  228.    /* linear filtering looks much nicer but is much slower for Mesa */
  229.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  230.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  231. #else
  232.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  233.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  234. #endif
  235.  
  236.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  237.  
  238.    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  239.  
  240.    if (!LoadRGBMipmaps("girl.rgb", GL_RGB)) {
  241.       printf("Error: couldn't load texture image\n");
  242.       exit(1);
  243.    }
  244.  
  245.  
  246.    /* setup textur env 1 */
  247. #ifdef GL_SGIS_multitexture
  248.    glSelectTextureSGIS(GL_TEXTURE1_SGIS);
  249. #endif
  250. #ifdef LINEAR_FILTER
  251.    /* linear filtering looks much nicer but is much slower for Mesa */
  252.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  253.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  254. #else
  255.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  256.    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  257. #endif
  258.  
  259.    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  260.  
  261.    if (!LoadRGBMipmaps("reflect.rgb", GL_RGB)) {
  262.       printf("Error: couldn't load texture image\n");
  263.       exit(1);
  264.    }
  265.  
  266.    glShadeModel(GL_FLAT);
  267.    glClearColor(0.3, 0.3, 0.4, 1.0);
  268.  
  269.    ModeMenu(TEXBOTH);
  270. }
  271.  
  272.  
  273. int main( int argc, char *argv[] )
  274. {
  275.    glutInit( &argc, argv );
  276.    glutInitWindowSize( 300, 300 );
  277.    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  278.    glutCreateWindow(argv[0] );
  279.  
  280.    Init();
  281.  
  282.    glutReshapeFunc( Reshape );
  283.    glutKeyboardFunc( Key );
  284.    glutSpecialFunc( SpecialKey );
  285.    glutDisplayFunc( Display );
  286.    glutIdleFunc( Idle );
  287.  
  288.    glutCreateMenu(ModeMenu);
  289.    glutAddMenuEntry("Texture 0", TEX0);
  290.    glutAddMenuEntry("Texture 1", TEX1);
  291.    glutAddMenuEntry("Multi-texture", TEXBOTH);
  292.    glutAddMenuEntry("Toggle Animation", ANIMATE);
  293.    glutAddMenuEntry("Quit", QUIT);
  294.    glutAttachMenu(GLUT_RIGHT_BUTTON);
  295.  
  296.    glutMainLoop();
  297.    return 0;
  298. }
  299.